package com.da.javatest.leetCode2;

import java.util.Arrays;

public class Code917 {
    public static void main(String[] args) {
        System.out.println(new Code917().reverseOnlyLetters("ab-cd"));
    }

    public String reverseOnlyLetters(String S) {
        char[] cs = S.toCharArray();
        int l = S.length() - 1;
        int index = 0;
        while (l >= index) {
            char c1 = cs[index];
            char c2 = cs[l];
            if (!((c1 > 64 && c1 < 91) || (c1 > 96 && c1 < 123))) {
                index++;
                continue;
            }
            if (!((c2 > 64 && c2 < 91) || (c2 > 96 && c2 < 123))) {
                l--;
                continue;
            }
            if (c1 == c2) {
                index++;
                l--;
                continue;
            }
            cs[index] = c2;
            cs[l] = c1;
            index++;
            l--;
        }
        return new String(cs);
    }
}
